package vip.zhenzicheng.design_patterns.prototype.test1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


/**
 * 深克隆(序列化)实现原型模式
 *
 * @author zhenzicheng
 * @date 2022-05-25 17:00
 */
public class CitationTest {
  private static final String FILE = "java-parent/src/main/resources/test/design_patterns/clone1.txt";

  public static void main(String[] args) throws Exception {
    //1，创建原型对象
    Citation citation = new Citation();
    //创建张三学生对象
    Student stu = new Student();
    stu.setName("张三");
    citation.setStu(stu);

    //创建对象输出流对象
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE));
    //写对象
    oos.writeObject(citation);
    //释放资源
    oos.close();

    //创建对象输入流对象
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE));
    //读取对象
    Citation citation1 = (Citation) ois.readObject();
    //释放资源
    ois.close();
    Student stu1 = citation1.getStu();
    stu1.setName("李四");

    citation.show();
    citation1.show();
  }
}
